package com.niit.spinner;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by CWork on 2019-5-23.
 */

public class spinnerBaseActivity extends Activity{
    private Spinner spinner1,spinner2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinnerbase);

        spinner1 = (Spinner)findViewById(R.id.spinnerBase);
        spinner2 = (Spinner)findViewById(R.id.spinnerBase1);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(spinnerBaseActivity.this,
                android.R.layout.simple_spinner_item,getData());
        adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);

        spinner1.setAdapter(adapter);
        spinner2.setAdapter(adapter);

        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(spinnerBaseActivity.this,
                        parent.getItemAtPosition(position).toString(),
                        Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });



    }

    private List<String> getData(){
        List<String> dataList = new ArrayList<String>();
        dataList.add("北京");
        dataList.add("上海");
        dataList.add("南京");
        dataList.add("宜昌");

        return dataList;
    }
}
